/* -*- mode: c -*-
 * Copyright (C) 2011 Lluís Vilanova <vilanova@ac.upc.edu>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef TF_TSE_H
#define TF_TSE_H

#include <unistd.h>
#include <stdint.h>

/** @file Task scheduling example.
 *
 * This example provides primitives for hardware-controlled task dependency
 * tracking and task execution selection.
 *
 * @note All task metadata is stored out of the @ref tf_mem memory area, but the
 *       storage for the actual input and output data is up to the software.
 */

#ifdef __cplusplus
extern "C" {
#endif

/** Maximum number of inputs for a single task. */
#define TF_TSE_MAX_INPUTS 2

/** Task identifier. */
typedef uint64_t tf_tse_id_t;

/** No tasks are left to execute. */
#define TF_TSE_ID_NONE ((tf_tse_id_t)0)
/** No task could be selected for execution (try later). */
#define TF_TSE_ID_BUSY ((tf_tse_id_t)1)

/** Initialize the thread scheduling example hardware.
 *
 * @param max Maximum total number of tasks in the system.
 */
void tf_tse_init (tf_tse_id_t max);

/** Create a new task.
 *
 * If @e nin is zero, the task will never be returned by @ref tf_tse_next, and
 * is immediately available for @ref tf_tse_finish.
 *
 * On error, @ref TF_TSE_ID_NONE is returned and @e errno is set appropiately.
 *
 * Errors:
 *  - EINVAL : invalid number of inputs
 *  - ENOMEM : tried to create too many tasks
 *
 * @return New task ID.
 */
tf_tse_id_t tf_tse_create (void * func, int nin, tf_tse_id_t * args);

/** Mark a task as executed.
 *
 * @pre The ID @e id has been previously returned by @ref tf_tse_next or was
 *      created with zero inputs (@ref tf_tse_create).
 */
void tf_tse_finish (tf_tse_id_t id);

/** Get the number of inputs of a task. */
int tf_tse_inputs (tf_tse_id_t id);

/** Get the function registered for a task. */
void * tf_tse_function (tf_tse_id_t id);

/** Get the ID of the nin'th input value of a task. */
tf_tse_id_t tf_tse_input (tf_tse_id_t id, int nin);

/** Get the next task that is ready to run.
 *
 * If the result is not @ref TF_TSE_ID_NONE or @ref TF_TSE_ID_BUSY, the returned
 * task is marked as running by the hardware (and thus it is expected that the
 * software will call @ref tf_tse_finish eventually).
 */
tf_tse_id_t tf_tse_next ();

#ifdef __cplusplus
}
#endif

#endif  /* TF_TSE_H */
